//
//  AddressEditVC.m
//  LoveTrail
//
//  Created by giantumbrella on 15/7/4.
//  Copyright (c) 2015年 赵娟. All rights reserved.
//

#import "AddressEditVC.h"
#import "Header.h"

@interface AddressEditVC ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong) UIButton *addressPickButton;
@property (strong, nonatomic) UIPickerView *myPicker;
@property (strong, nonatomic) UIView *pickerBgView;
@property (strong, nonatomic) UIView *maskView;
//data
@property (strong, nonatomic) NSDictionary *pickerDic;
@property (strong, nonatomic) NSArray *provinceArray;
@property (strong, nonatomic) NSArray *cityArray;
@property (strong, nonatomic) NSArray *townArray;
@property (strong, nonatomic) NSArray *selectedArray;

@property (nonatomic,strong) UIButton *cancelButton;
@property (nonatomic,strong) UIButton *ensureButton;


@end

@implementation AddressEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView: @"编辑个人信息"];

    self.view.backgroundColor = [UIColor whiteColor];
    [self getPickerData];
       //右侧按钮
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(saveDataAndBackAddress:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
   
   
    self.addressPickButton = [[UIButton alloc] initWithFrame:CGRectMake(10,70, SCREEN_WIDTH-20, 40)];
    [self.view addSubview:self.addressPickButton];
    self.addressPickButton.backgroundColor = [UIColor whiteColor];
    self.addressPickButton.layer.borderColor = [[UIUtil colorWithHexString:@"#e3e3e3"] CGColor];
    [self.addressPickButton setTitleColor:[UIUtil colorWithHexString:@"#939393"] forState:UIControlStateNormal];
    self.addressPickButton.layer.borderWidth = 1;
    [self.addressPickButton setTitle:self.pathValueString forState:UIControlStateNormal];
    [self.addressPickButton addTarget:self action:@selector(showMyPicker:) forControlEvents:UIControlEventTouchUpInside];
    
    //创建控件 picker
    [self createPikerAndAddressPickLable];
}


#pragma mark 保存 返回上
- (void)saveDataAndBackAddress:(NSString *)valueButtonTitle
{
    if ([self.addressDelegate respondsToSelector:@selector(saveAddressAndBackValue:)]) {
        [self.addressDelegate saveAddressAndBackValue:self.addressPickButton.titleLabel.text];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 创建picker 接收数据label
- (void)createPikerAndAddressPickLable
{
  
    self.pickerBgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, 320, 280)];
    [self.view addSubview:self.pickerBgView];
    self.pickerBgView.backgroundColor = [UIColor whiteColor];

//    //请选择所在城市
//    UILabel *titelLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, self.pickerBgView.frame.size.width, 20)];
//    [self.pickerBgView addSubview:titelLabel];
//    titelLabel.text = @"请选择所在城市";
//    titelLabel.textColor = [UIUtil colorWithHexString:@"#464646"];
//    titelLabel.font = [UIFont boldSystemFontOfSize:18];
//
//    //第一根线
//    UIView *lineViewFirst = [[UIView alloc] initWithFrame:CGRectMake(0, titelLabel.frame.size.height+titelLabel.frame.origin.y*2, SCREEN_WIDTH, 1)];
//    [self.pickerBgView addSubview:lineViewFirst];
//    lineViewFirst.backgroundColor = [UIUtil colorWithHexString:@"#ea5073"];
    
    //我的picker
    self.myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
    [self.pickerBgView addSubview:self.myPicker];
    self.myPicker.dataSource = self;
    self.myPicker.delegate = self;
//    self.myPicker.backgroundColor = [UIColor redColor];
    
//    //框框线
//    UILabel *kuangLabel = [[UILabel alloc] initWithFrame:CGRectMake(1, self.myPicker.frame.origin.y+self.myPicker.frame.size.height*0.2, SCREEN_WIDTH-2,70)];
//    [self.myPicker addSubview:kuangLabel];
//    kuangLabel.layer.borderColor = [UIUtil colorWithHexString:@"#ea5073"].CGColor;
//    kuangLabel.layer.borderWidth = 1;
    
    //取消button
    self.cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.myPicker.frame.size.height, SCREEN_WIDTH/2, 40)];
    [self.pickerBgView addSubview:self.cancelButton];
    [self.cancelButton addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    //    self.cancelButton.backgroundColor = [UIColor redColor];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    self.cancelButton.layer.borderColor = [UIUtil colorWithHexString:@"#dddddd"].CGColor;
    self.cancelButton.layer.borderWidth = 1;
    [self.cancelButton setTitleColor:[UIUtil colorWithHexString:@"#787878"] forState:UIControlStateNormal];

    
    self.ensureButton = [[UIButton alloc] initWithFrame:CGRectMake(self.cancelButton.frame.origin.x+self.cancelButton.frame.size.width, self.cancelButton.frame.origin.y,  SCREEN_WIDTH/2, 40)];
    [self.pickerBgView addSubview:self.ensureButton];
    [self.ensureButton addTarget:self action:@selector(ensure:) forControlEvents:UIControlEventTouchUpInside];
    //    self.ensureButton.backgroundColor = [UIColor greenColor];
    [self.ensureButton setTitle:@"确定" forState:UIControlStateNormal];
    self.ensureButton.layer.borderColor = [UIUtil colorWithHexString:@"#dddddd"].CGColor;
    self.ensureButton.layer.borderWidth = 1;
    [self.ensureButton setTitleColor:[UIUtil colorWithHexString:@"#787878"] forState:UIControlStateNormal];
    
    [self initView];

}



#pragma mark - init view
- (void)initView {
    
    self.maskView = [[UIView alloc] initWithFrame:kScreen_Frame];
    self.maskView.backgroundColor = [UIColor blackColor];
    self.maskView.alpha = 0;
    [self.maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMyPicker)]];
    
    self.pickerBgView.width = SCREEN_WIDTH;
}

#pragma mark - get data
- (void)getPickerData {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    self.pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.provinceArray = [self.pickerDic allKeys];
    self.selectedArray = [self.pickerDic objectForKey:[[self.pickerDic allKeys] objectAtIndex:0]];
    
    if (self.selectedArray.count > 0) {
        self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
    }
    
    if (self.cityArray.count > 0) {
        self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
    }
    
}

#pragma mark - UIPicker Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.provinceArray.count;
    } else if (component == 1) {
        return self.cityArray.count;
    } else {
        return self.townArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return [self.provinceArray objectAtIndex:row];
    } else if (component == 1) {
        return [self.cityArray objectAtIndex:row];
    } else {
        return [self.townArray objectAtIndex:row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 110;
    } else if (component == 1) {
        return 100;
    } else {
        return 110;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedArray = [self.pickerDic objectForKey:[self.provinceArray objectAtIndex:row]];
        if (self.selectedArray.count > 0) {
            self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
        } else {
            self.cityArray = nil;
        }
        if (self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        } else {
            self.townArray = nil;
        }
    }
    [pickerView selectedRowInComponent:1];
    [pickerView reloadComponent:1];
    [pickerView selectedRowInComponent:2];
    
    if (component == 1) {
        if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
            self.townArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:row]];
        } else {
            self.townArray = nil;
        }
        [pickerView selectRow:1 inComponent:2 animated:YES];
    }
    
    [pickerView reloadComponent:2];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 50;
}

#pragma mark - private method
- (void)showMyPicker:(id)sender {
    [self.view addSubview:self.maskView];
    [self.view addSubview:self.pickerBgView];
    self.maskView.alpha = 0;
    self.pickerBgView.top = self.view.height;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.maskView.alpha = 0.3;
        self.pickerBgView.bottom = self.view.height;
    }];
}

- (void)hideMyPicker {
    [UIView animateWithDuration:0.3 animations:^{
        self.maskView.alpha = 0;
        self.pickerBgView.top = self.view.height;
    } completion:^(BOOL finished) {
        [self.maskView removeFromSuperview];
        [self.pickerBgView removeFromSuperview];
    }];
}

#pragma mark -

- (void)cancel:(id)sender {
    [self hideMyPicker];
}

- (void)ensure:(id)sender {
    [self.addressPickButton setTitle:[NSString stringWithFormat:@"%@%@%@",[self.provinceArray objectAtIndex:[self.myPicker selectedRowInComponent:0]],[self.cityArray objectAtIndex:[self.myPicker selectedRowInComponent:1]], [self.townArray objectAtIndex:[self.myPicker selectedRowInComponent:2]]] forState:UIControlStateNormal];
    [self hideMyPicker];
}

@end
